package cn.walls1717.mall.modules.pms.service.impl;

import cn.walls1717.mall.modules.pms.dto.PageListDTO;
import cn.walls1717.mall.modules.pms.model.PmsBrand;
import cn.walls1717.mall.modules.pms.mapper.PmsBrandMapper;
import cn.walls1717.mall.modules.pms.service.PmsBrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    private final PmsBrandMapper brandMapper;
    @Autowired
    public PmsBrandServiceImpl(PmsBrandMapper brandMapper) {
        this.brandMapper = brandMapper;
    }

    @Override
    public Page<PmsBrand> pageList(PageListDTO pageListDTO) {
        Page<PmsBrand> page = new Page<>(pageListDTO.getPageNum(), pageListDTO.getPageSize());
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        if (!Strings.isEmpty(pageListDTO.getKeyword())) {
            lqw.like(PmsBrand::getName, pageListDTO.getKeyword());
        }
        // 根据sort倒序排序
        lqw.orderByDesc(PmsBrand::getSort);
        brandMapper.selectPage(page, lqw);
        return page;
    }

    @Override
    public Boolean updateStatus(List<Long> ids, PmsBrand brand) {
        List<PmsBrand> brands = this.listByIds(ids);
        for (PmsBrand bd : brands) {
            // 给用于修改的临时实体对象设置id，用于根据id修改
            brand.setId(bd.getId());
            if (!this.updateById(brand)) {
                return false;
            }
        }
        return true;
    }
}
